import java.util.*;

public class Solution {
    class TreeNode {
        int val = 0;
        TreeNode left = null;
        TreeNode right = null;

        public TreeNode(int val) {
            this.val = val;

        }

    }

    TreeNode pre = null;
    TreeNode ret = null;

    void dfs(TreeNode root){
        if(root == null){
            return;
        }

        dfs(root.left);

        if(pre == null){
            pre = root;
            ret = root;
        }else{
            pre.right = root;
            root.left = pre;
            pre = root;
        }

        dfs(root.right);



    }

    public TreeNode Convert(TreeNode root) {
        dfs(root);
        return ret;
    }
}
